package com.bytz.modules.cms.order.service.callback;

import com.boya.cup.flowable.service.model.request.FetchAssignCallBackRo;
import com.boya.cup.flowable.service.model.response.FetchAssignResult;
import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.bpm.client.service.ILocalCustomAssignCallbackService;
import com.bytz.modules.bpm.client.util.GetAuditPageInfoUtil;
import com.bytz.modules.cms.order.constants.OrderFlowConstants;
import com.bytz.modules.cms.order.entity.Order;
import com.bytz.modules.cms.order.service.IOrderService;
import com.bytz.modules.cms.reseller.service.impl.EndUserServiceImpl;
import com.bytz.modules.cms.user.entity.CmsSysUser;
import com.bytz.modules.cms.user.service.ICmsSysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: cms-backend
 *
 * @description:
 *
 * @author: KyleWang
 *
 * @create: 2022-11-09 16:17
 **/

@Component
public class OrderAssigneeCallbackService implements ILocalCustomAssignCallbackService {

    @Autowired
    ICmsSysUserService cmsSysUserService;
    @Autowired
    IOrderService orderService;
    @Autowired
    GetAuditPageInfoUtil auditPageInfoUtil;

    @Autowired
    EndUserServiceImpl endUserService;


    @Override
    public List<FetchAssignResult.Resource> fetchAuditResources(FetchAssignCallBackRo fetchAssignCallBackRo, Map<String, Object> parameters) {
        String type = (String) parameters.get(OrderFlowConstants.OrderAssigneeConstants.Parameters.TYPE);
        String roleName = (String) parameters.get(OrderFlowConstants.OrderAssigneeConstants.Parameters.ROLE_NAME);
        String roleCode = (String) parameters.get(OrderFlowConstants.OrderAssigneeConstants.Parameters.ROLE_CODE);
        String roleId = (String) parameters.get(OrderFlowConstants.OrderAssigneeConstants.Parameters.ROLE_ID);
        String deptName = (String) parameters.get(OrderFlowConstants.OrderAssigneeConstants.Parameters.DEPT_NAME);
        String deptId = (String) parameters.get(OrderFlowConstants.OrderAssigneeConstants.Parameters.DEPT_Id);

        List<FetchAssignResult.Resource> resources = Collections.emptyList();
        if (StringUtils.equals(type, OrderFlowConstants.OrderAssigneeConstants.Parameters.TYPE_AREA)) {
            Order order = orderService.getById(fetchAssignCallBackRo.getBusinessKey());
            if (order == null) {
                throw new BytzBootException("找不到订单");
            }
            String salesId = order.getSalesId();
            CmsSysUser seller = cmsSysUserService.getById(salesId);
            if (seller != null) {
                List<CmsSysUser> userByRoleAndDept = cmsSysUserService.getUserByRoleAndDept(roleName, roleCode, roleId, seller.getCurrentDeptId(), null);
                resources = userByRoleAndDept.stream().filter(Objects::nonNull)
                        .filter(user -> StringUtils.equals(StringUtils.trimToNull(seller.getArea()), StringUtils.trimToNull(user.getArea())))
                        .map(this::toResource)
                        .collect(Collectors.toList());

            }

        } else if (StringUtils.equals(type, OrderFlowConstants.OrderAssigneeConstants.Parameters.TYPE_DEPT)) {
            List<CmsSysUser> userByRoleAndDept = cmsSysUserService.getUserByRoleAndDept(roleName, roleCode, roleId, deptId, deptName);
            resources = userByRoleAndDept.stream()
                    .filter(Objects::nonNull)
                    .map(this::toResource)
                    .collect(Collectors.toList());
        }
        if (CollectionUtils.isEmpty(resources)) {
            throw new BytzBootException("找不到处理人");
        }
        return resources;
    }
}